import styleList from '@/common/js/styleList.js';

let commonEopts = {
  tooltip: {
    ...styleList['tooltip1'],
    axisPointer: {
      type: 'shadow',
    }
  },
  grid: {
    left: '3%',
    right: '10%',
    bottom: '4%',
    containLabel: true,
  },
  legend: {
    itemWidth: 18,
    itemHeight: 12,
    bottom: 20,
    textStyle: {
      fontSize: 11,
      color: '#fff',
    },
  },
}
// || (item.title.indexOf('增速') != -1 && e11.targetName == '同比')
export const handleThirdChar = function (itemData, codeFen, title) {
  let lunit = itemData.find(e => e.style == 11).data[0].unit
  let iszs = false
  let lg1w = false
  let iszsGt400 = false
  let iszsLt400 = false

  let categories = []

  let chartData = {
    series: []
  }
  
  // 特殊处理 相反
  let reverse = true
  if(title && title=='各地市商用转保保费进度'){
    reverse = false
  }

  let isQd = (title.indexOf('渠道') != -1)  //是否是渠道  取targetValue

  itemData.forEach((el,idx) => {
    if (el.style == '11') {
      let seriesq = {
        type: 'bar',
        ...styleList[11],
        barMinHeight: 5,
        name: (el.data[0].targetName == '增速') || (el.data[0].targetName == '同比') ? el.data[0].targetName + '正' :
              el.data[0].targetName + '超目标' ,//: el.data[0].targetName + ' ',
        itemStyle: {
          color: reverse ? "#1266FF" : "#FFB07B"
        },
        data: []
      }
      let seriesh = {
        type: 'bar',
        ...styleList[11],
        barMinHeight: 5,
        name: (el.data[0].targetName == '增速') || (el.data[0].targetName == '同比') ? el.data[0].targetName + '负' :
              el.data[0].targetName + '落后目标',// : el.data[0].targetName + '  ',
        itemStyle: {
          color: reverse ? "#FFB07B" : "#1266FF"
        },
        data: []
      }
      el.data.forEach(e11 => {
        iszs = (e11.targetName == '增速') || (title.indexOf('增速') != -1 && e11.targetName == '同比')
        
        if (!isQd && codeFen =='000A') {
          if (e11.score !== undefined && e11.score !== null) {
            if (e11.score >= 0 || isNaN(e11.score)) {
              if (!lg1w && !iszs) {
                lg1w = (e11.score > 10000)
              }
              if (!iszsGt400) {
                iszsGt400 = (e11.score > 400 && iszs)
              }
              e11.score = (e11.score > 400 && iszs) ? 400 : e11.score
              seriesq.data.push({
                value: isNaN(e11.score) ? 0 : e11.score,
                reValue: e11.score,
                u: lunit
              })
              seriesh.data.push(null)
            } else {
              if (!lg1w && !iszs) {
                lg1w = (e11.score < -10000)
              }
              if (!iszsLt400) {
                iszsLt400 = (e11.score < -400 && iszs)
              }
              e11.score = (e11.score < -400 && iszs) ? -400 : e11.score
              seriesq.data.push(null)
              seriesh.data.push({value: e11.score, u: lunit})
            }
          } else {
            seriesh.data.push(null)
            seriesq.data.push(null)
          }
        } else {
          if (e11.targetValue !== undefined && e11.targetValue !== null) {
            if (e11.targetValue >= 0 || isNaN(e11.targetValue)) {
              if (!lg1w && !iszs) {
                lg1w = (e11.targetValue > 10000)
              }
              if (!iszsGt400) {
                iszsGt400 = (e11.targetValue > 400 && iszs)
              }
              e11.targetValue = (e11.targetValue > 400 && iszs) ? 400 : e11.targetValue
              seriesq.data.push({
                value: isNaN(e11.targetValue) ? 0 : e11.targetValue,
                reValue: e11.targetValue,
                u: lunit
              })
              seriesh.data.push(null)
            } else {
              if (!lg1w && !iszs) {
                lg1w = (e11.targetValue < -10000)
              }
              if (!iszsLt400) {
                iszsLt400 = (e11.targetValue < -400 && iszs)
              }
              e11.targetValue = (e11.targetValue < -400 && iszs) ? -400 : e11.targetValue
              seriesq.data.push(null)
              seriesh.data.push({value: e11.targetValue, u: lunit})
            }
          } else {
            seriesh.data.push(null)
            seriesq.data.push(null)
          }
        }
      })
      chartData.series = reverse ? [seriesq, seriesh] : [seriesh, seriesq]
    } else if (el.style == '14') {
      categories = el.data.map(e3 => e3.x)
    }
  })
  
  let eopts = {
    ...JSON.parse(JSON.stringify(commonEopts)),
    xAxis: [
      {
        name: "单位:" + lunit,
        nameLocation: "end",
        nameTextStyle: {
          verticalAlign: "bottom",
          color: "#ffffff",
          padding: [0, 0, 30, -50],
          fontSize: 12,
        },
        type: "value",
        splitNumber: lg1w ? 2 : 3,
        position: "top",
        axisLabel: {
          color: "#ffffff",
          fontSize: lg1w ? 10 : 11,
          align: 'center',
        },
        splitLine: {
          lineStyle: {
            color: "#ffffff",
            opacity: 0.1
          }
        }
      }
    ],
    yAxis: [
      {
        type: "category",
        axisTick: {
          show: false
        },
        axisLabel: {
          color: "#ffffff",
          interval: 0,
          fontSize: 11
        },
        data: categories,
      }
    ],
    legend: {
      data: chartData.series.map(e => e.name),
      textStyle: {
        fontSize: 12,
        color: "#24e8ff",
      },
      itemWidth: 16,
      itemHeight: 10,
      top: 3
    }
  }

  iszsLt400 && (eopts.xAxis[0].min = -450)
  iszsGt400 && (eopts.xAxis[0].max = 450)

  let ltgt400 = (iszsLt400 || iszsGt400)
  
  return {
    chartData, eopts,
    height: categories.length * 56,
    iszs: ltgt400 && iszs
  }
}
